package cn.glc.daily.life.web.controller.manageapi;

import cn.glc.daily.life.common.constans.Developers;
import cn.glc.daily.life.common.utils.JsonResult;
import cn.glc.daily.life.model.dto.LoginDto;
import cn.glc.daily.life.model.dto.UserAddOrUpdateDto;
import cn.glc.daily.life.model.dto.UserSignupDto;
import cn.glc.daily.life.web.service.user.UserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 登录/登出/主页/异常
 *
 * @author simon
 * @date 2020/9/18 15:17
 * @copyright:
 * @modifier
 * @motto a no talent, people who have to study hard
 */
@Slf4j
@RestController
@RequestMapping
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 登录/统一登录
     * ps: 第三方（微信,微博等）很特殊,登录包含注册和登录
     * ps: 微信的没有搞,注册都要收费,蛋疼
     * 1. 第三方登陆时,如果账号已经存在,就把当前唯一值,当做密码更新（例如:微博网页登陆时,会生成code唯一值）
     * 2. 其他平台待定
     *
     * @param loginDto
     * @return
     */
    @PostMapping(value = "/login")
    @ApiOperation(value = "用户登录", notes = Developers.CY)
    public JsonResult login(@RequestBody @Valid LoginDto loginDto) {
        try {
            return userService.unifiedLogin(loginDto);
        } catch (Exception e) {
            return JsonResult.of(e);
        }
    }

    /**
     * 注册用户
     *
     * @param userDto
     * @return
     */
    @PostMapping(value = "/signup")
    @ApiOperation(value = "用户注册", notes = Developers.CY)
    public JsonResult signup(@RequestBody @Valid UserSignupDto userDto) {
        return JsonResult.returnSuccess(userService.signup(userDto));
    }

    /**
     * 登出
     *
     * @param userDto
     * @return
     */
    @PostMapping(value = "/logout")
    @ApiOperation(value = "用户登出", notes = Developers.CY)
    public JsonResult logout(@RequestBody UserAddOrUpdateDto userDto) {
        return JsonResult.returnSuccess("登出成功");
    }

    /**
     * 主页
     *
     * @return
     */
    @GetMapping(value = "/index")
    public JsonResult index() {
        return JsonResult.returnSuccess("跳转到主页");
    }

    /**
     * 错误页面
     *
     * @return
     */
    @GetMapping(value = "/error")
    public JsonResult error() {
        return JsonResult.returnSuccess("登录出错了,请重新登录");
    }
}
